// wigets/business/stockdistribution.js
import {
  list,
  detail
} from '../../../js/modules/mall';
import {
  api,
  util,
  dialog,
  forms,
  storage
} from '../../../js/util';
var app = getApp();
const componentBehavior = require('../../../js/common/ComponentBehavior')

Component({
  behaviors: [componentBehavior],
  lifetimes: {
    attached() {
      // 在组件实例进入页面节点树时执行
    },
    detached() {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  /**
   * 组件的属性列表
   */
  properties: {
    product:{
      value:{},
      type:Object
    }
  },

  /**
   * 页面的初始数据
   */
  data: {
    isIphoneX: app.globalData.isIphoneX,
    imgDomain: api.imgDomain,
    enableOss: api.enableOss,
    isGoodsStock: false,
    goodsList: [],
    goodsStocks: [],
    stockList: []
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /*
      弹出库存分布的层 
    */
    showStockModal(e) {
      var that = this;
      if (this.data.isGoodsStock) {
        this.getGoodsStockDistribution(() => {
          dialog.showModal(that, 'showModalStatus')
        });
      } else {
        this.getGoodsList();
        this.getStockDistribution(() => {
          dialog.showModal(that, 'showModalStatus')
        });
      }
    },
    /**隐藏库存分布的层 */
    hideModal() {
      var that = this;
      dialog.hideModal(that, 'showModalStatus')
    },
    getGoodsList(){
      var that = this;
      let param = {
        productId: this.data.product.ProductId,
        promotions: this.data.product.Promotions
      }
      detail.loadSpecific(param, (goodSpecific, specifictions, units, content)=> {
        that.setData({goodsList: goodSpecific}, ()=>{
          that.bindGoodsStock();
        });
      });
    },
    getStockDistribution(callback){
      var that = this;
      let args = {
        fkFlag: this.data.product.FKFlag,
        fkId: this.data.product.FKId, 
        productId: this.data.product.ProductId
      }
      list.getProductStockDistribution(args, (res) => {
        that.setData({goodsStocks: res}, () => {
          if (callback&&typeof(callback)=='function') {
            callback();
          }
          that.bindGoodsStock();
        });
      });
    },
    bindGoodsStock(){
      let { goodsList, goodsStocks } = this.data;
      let stockList = [];
      outer:for (let i = 0; i < goodsStocks.length; i++) {
          let item = goodsStocks[i];
          if (item.Stock>0||item.UseStock>0) {
              inner:for (let i = 0; i < goodsList.length; i++) {
                  let goods = goodsList[i];
                  if (goods.GoodsId == item.GoodsId) {
                      item.SpecValue = goods.SpecValue;
                      item.Unit = goods.Unit;
                      break inner;
                  }
              }
              stockList.push(item);
          }
      }
      this.setData({stockList: stockList});
    },
    getGoodsStockDistribution(callback){
      var that = this;
      let args = {
        fkFlag: this.data.product.FKFlag,
        fkId: this.data.product.FKId, 
        goodsId: this.data.product.GoodsId
      }
      list.getGoodsStockDistribution(args, (res) => {
        let stockList = [];
        if (res && res.length > 0) {
          for (let i = 0; i < res.length; i++) {
              let item = res[i];
              if (item.Stock>0||item.UseStock>0) {
                item.Unit = that.data.product.Unit;
                stockList.push(item);
              }
          }
        }
        that.setData({stockList: stockList}, callback);
      });
    },
  }
})